﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor.app
{
    /// <summary>
    /// RSpriteActionStep.xaml 的交互逻辑
    /// </summary>
    public partial class RSpriteActionStep : UserControl
    {
        public Action OnCloseCallback = null;

        public RSpriteActionStep()
        {
            InitializeComponent();

            btnClose.Click += new RoutedEventHandler(btnClose_Click);
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (OnCloseCallback != null)
            {
                OnCloseCallback();
            }
        }

        public void Load(comm.objst.RPack rpack, comm.objst.RSprite sprite)
        {
            var endTime1 = sprite.GetGroupEndTime(rpack, comm.objst.RSprite.StartType.ZERO);
            var endTime2 = sprite.GetGroupEndTime(rpack, comm.objst.RSprite.StartType.END);
            var timeList = new List<uint?[]>();
            var execTimeNull = false;

            spList.Children.Clear();

            var group = sprite.GetGroup();
            var cList = new List<string>();
            foreach (var item in group.ElementDic.Values)
            {
                comm.objst.RSprite.Timeline tl = null;
                if (item is comm.objst.RSprite.Timeline && (tl = (comm.objst.RSprite.Timeline)item).StartType == comm.objst.RSprite.StartType.ZERO)
                {
                    if (tl.EndTime == null && tl.MaxLoopCount == null)
                    {
                        execTimeNull = true;
                    }
                    var ts = new uint?[] { tl.BeginTime, endTime1 - tl.BeginTime, endTime2 };
                    var key = ts[0] + "," + ts[1] + "," + ts[2];
                    if (!cList.Contains(key))
                    {
                        cList.Add(key);
                        timeList.Add(ts);
                    }
                }
            }
            if (execTimeNull)
            {
                foreach (var item in timeList)
                {
                    item[1] = null;
                }
            }
            if (timeList.Count == 0)
            {
                timeList.Add(new uint?[] { 0, null, endTime2 });
            }

            spList.Width = endTime1 + endTime2;

            foreach (var ts in timeList)
            {
                var ts2 = ts;
                var sp = new StackPanel { Orientation = Orientation.Horizontal, Height = 20, Margin = new Thickness(0, 0, 0, 4) };
                sp.MouseLeftButtonDown += new MouseButtonEventHandler((o, e) =>
                {
                    sprite.BeginTime = ts2[0].Value;
                    sprite.ExecTime = ts2[1];
                    sprite.EndTime = ts[2].Value;
                    if (OnCloseCallback != null)
                    {
                        OnCloseCallback();
                    }
                });
                sp.Children.Add(new Border
                {
                    Width = ts[0].Value,
                    Child = new TextBlock { Text = ts[0].Value.ToString() },
                    Background = new SolidColorBrush(Color2.Orange(50))
                });
                sp.Children.Add(new Border
                {
                    Width = endTime1 - ts[0].Value,
                    Child = new TextBlock { Text = ts[1].HasValue ? ts[1].Value.ToString() : "null" },
                    Background = new SolidColorBrush(Color2.Green(50))
                });
                sp.Children.Add(new Border
                {
                    Width = ts[2].Value,
                    Child = new TextBlock { Text = ts[2].Value.ToString() },
                    Background = new SolidColorBrush(Color2.Black(50))
                });
                spList.Children.Add(sp);
            }
        }
    }
}
